//
//  BaseAnnotationView.m
//  markmap
//
//  Created by 甘世清 on 2024/3/25.
//

#import "BaseAnnotationView.h"
#import "PointPopView.h"
@implementation BaseAnnotationView

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self. bgImage = [[UIImageView alloc]init];
        self.bgImage.image = SQGetImage(@"定位");
        [self addSubview: self.bgImage];
        [ self.bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(self);
            make.width.mas_equalTo(46);
            make.height.mas_equalTo(60);
        }];
        [ self.bgImage addSubview:self.headerView];
        [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo( self.bgImage).offset(3);
            make.centerX.equalTo(self.bgImage);
            make.size.mas_equalTo(40);
        }];
        
        
        self.bgView = [[UIView alloc]init];
        self.bgView.layer.cornerRadius = 6;
        self.bgView.layer.masksToBounds = YES;
        self.bgView.backgroundColor = UIColor.whiteColor;
        [self addSubview:self.bgView];

        [self.bgView addSubview:self.numberLabel];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bgView);
            make.right.equalTo(self.bgView.mas_right).offset(-6);
        }];

        [self.bgView addSubview:self.likeView];
        [self.likeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bgView);
            make.right.equalTo(self.numberLabel.mas_left).offset(-2);
            make.size.mas_equalTo(24);
        }];

        [self.bgView addSubview:self.nameLabel];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bgView);
            make.right.equalTo(self.likeView.mas_left).offset(-2);
            make.height.mas_equalTo(30);
        }];

        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.left.equalTo(self.nameLabel.mas_left).offset(-6);
            make.height.mas_equalTo(40);
            make.bottom.equalTo( self.bgImage.mas_top).offset(-4);
        }];

        [self addSubview:self.addressLabel];
        [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.bgView.mas_top).offset(-4);
            make.width.mas_equalTo(120);
        }];


        self.btn  = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.backgroundColor = UIColor.clearColor;
        [self.btn addTarget:self action:@selector(SQ_ButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.btn];
        [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.bgView).insets(UIEdgeInsetsMake(0, 0, -60, 0));
        }];
    }
    return self;
}
- (void)setModel:(PointModel *)model{
    _model = model;
    self.addressLabel.text = model.address;
    self.nameLabel.text = [NSString stringWithFormat:@"%@",model.title];
    self.numberLabel.text = [NSString stringWithFormat:@"%d",model.likeNum];
    [self.headerView sd_setImageWithURL:SQURLImage(model.portrait) placeholderImage:SQGetImage(@"zanwutouxiang")];
    if (model.isLike) {
        self.likeView.image = SQGetImage(@"yidianzan");
    } else {
        self.likeView.image = SQGetImage(@"arrow");
    }
}
- (void)setShow:(BOOL)show{
    _show = show;
    self.bgView.hidden = show;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        CGPoint tempoint = [self.btn convertPoint:point fromView:self];
        if (CGRectContainsPoint(self.btn.bounds, tempoint))
        {
            view = self.btn;
        }
    }
    return view;
}
-(void)SQ_ButtonClick{
    if (self.show) {
    PeopleDetailViewController * vc = [[PeopleDetailViewController alloc]init];
    vc.uid = self.model.userid;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
    } else {
        [self SQ_popView];
    }
}
-(void)SQ_popView{
    PointPopView * vc = [[PointPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 400)];
    [vc SQ_dataForId:self.model.userid];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:[Manager SQ_getCurrentViewController].view
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
      pop.hemStyle = LSTHemStyleBottom;
      [pop pop];
      LSTPopViewWK(pop)
      [pop setBgClickBlock:^{
          [wk_pop dismiss];
      }];
    WS(weakSelf);
    [vc setIsLikeBtnBlock:^(PointModel * _Nonnull model) {
        weakSelf.numberLabel.text = [NSString stringWithFormat:@"%d",model.likeNum];
        if (model.isLike) {
            weakSelf.likeView.image = SQGetImage(@"yidianzan");
        } else {
            weakSelf.likeView.image = SQGetImage(@"arrow");
        }
    }];
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.backgroundColor = UIColor.clearColor;
        _nameLabel.font = FONT_MEDIUM(16);
        _nameLabel.textColor = UIColor.blackColor;
    }
    return _nameLabel;
}
- (UILabel *)numberLabel{
    if (!_numberLabel) {
        _numberLabel = [[UILabel alloc]init];
        _numberLabel.textAlignment = NSTextAlignmentCenter;
        _numberLabel.backgroundColor = UIColor.clearColor;
        _numberLabel.font = FONT_MEDIUM(13);
        _numberLabel.textColor = SQColor(@"#E133FF");
    }
    return _numberLabel;
}
- (UILabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc]init];
        _addressLabel.font = FONT_REGULAR(12);
        _addressLabel.textColor = UIColor.whiteColor;
        _addressLabel.numberOfLines = 0;
        _addressLabel.hidden = YES;
        _addressLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _addressLabel;
}
- (UIImageView *)headerView{
    if (!_headerView) {
        _headerView = [[UIImageView alloc]init];
        _headerView.contentMode = UIViewContentModeScaleAspectFill;
        _headerView.clipsToBounds = YES;
        _headerView.layer.cornerRadius = 20;
        _headerView.layer.masksToBounds = YES;
    }
    return _headerView;
}
- (UIImageView *)likeView{
    if (!_likeView) {
        _likeView = [[UIImageView alloc]init];
        _likeView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _likeView;
}
@end
